package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.constant.LkdConstant;
import com.chen.mapper.OrderMapper;
import com.chen.mapper.RegionMapper;
import com.chen.pojo.common.PageResult;
import com.chen.pojo.entry.Orders;
import com.chen.pojo.entry.Region;
import com.chen.pojo.vo.OrderPartnerVo;
import com.chen.pojo.vo.ReportVo;
import com.chen.service.ReportService;
import com.chen.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/20
 * 统计业务实现类
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 获取指定时间内的订单总量
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 结果
     */
    @Override
    public Long getOrderCount(LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(start != null && end != null, Orders::getCreateTime, start, end);
        return orderMapper.selectCount(wrapper);
    }

    /**
     * 获取指定时间内的订单总销售额
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 结果
     */
    @Override
    public BigDecimal getOrderAmount(LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(start != null && end != null, Orders::getCreateTime, start, end);
        wrapper.select(Orders::getAmount);
        List<Orders> orders = orderMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(orders)) {
            return orders.stream().map(Orders::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return BigDecimal.ZERO;
    }

    /**
     * 根据地区汇总销售额数据(销售量分布)
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return 结果
     */
    @Override
    public ReportVo getRegionCollectAmount(LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Orders::getCreateTime, start, end);
        List<Orders> orders = orderMapper.selectList(wrapper);
        Map<String, BigDecimal> orderMap = orders.stream()
                .collect(Collectors.toMap(Orders::getRegionId, Orders::getAmount));
        // 获取地域列表
        Stream<String> nameList = regionMapper.selectBatchIds(orderMap.keySet()).stream().map(Region::getName);
        // 结果编辑
        ReportVo reportVo = new ReportVo();
        reportVo.setXAxis(nameList.toArray(String[]::new));
        reportVo.setSeries(orderMap.values().toArray(BigDecimal[]::new));
        return reportVo;
    }

    @Override
    public BigDecimal getTotalBill(LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Orders::getCreateTime, start, end);
        wrapper.eq(Orders::getStatus, LkdConstant.ORDER_PAY_COMPLETED);
        List<Orders> orders = orderMapper.selectList(wrapper);
        return orders.stream().map(Orders::getBill).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    @Override
    public PageResult<OrderPartnerVo> getPartnerCollect(Integer pageIndex, Integer pageSize, LocalDate start, LocalDate end, String partnerName) {
        Page<OrderPartnerVo> page = new Page<>(pageIndex, pageSize);
        page = orderMapper.selectPartnerCollect(page,start, end, partnerName);
        return PageUtils.mapperResult(page);
    }
}
